package mm1.GUI; 


import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.layout.Resizable;
import javafx.scene.layout.VBox;
import javafx.scene.Group;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.control.ProgressBar;
import javafx.lang.Duration;
import javafx.scene.input.MouseEvent;

/**
 * Implementation and Layout of the media player and the progress bar
 *
 * @author Ralph Hoch
 */

public class VideoScreen extends CustomNode, Resizable {

	public var movieWidth:Number;
	public var movieHeight:Number;
	public var volume:Number;

	var media: Media = Media {
		//duration: Duration.valueOf(1)
	}
	
	var videoSrc:String on replace old {
	    media = Media {
	        source: videoSrc
	    }
	}
	
	var progressBar:ProgressBar = ProgressBar {
	    progress: bind 
	    			if (media.duration == Duration.valueOf(0)) {
						0
					}
					else {
						((player.currentTime) / media.duration)
					}
	    width: bind movieWidth
	}
	
	public var centerPos:Boolean;
	
	public var player = MediaPlayer{
	    media: bind media;
	    autoPlay: false
	    paused: true
	    volume: bind volume
	};
	
	var mediaView:MediaView = MediaView {
        visible: true
        preserveRatio: true
    	mediaPlayer: bind player
    	fitHeight: bind movieHeight
    	fitWidth: bind movieWidth
    	onMouseClicked: function(me:MouseEvent) {
    	    if (centerPos == true) {
	    	    println("CLICKED_X: {me.screenX}");
	    	    println("CLICKED_Y: {me.screenY}");
    	    }
    	}
	}
	
	
	/**
	 * Create the Node
	 */
	public override function create():Node {
		return Group{
			content: [
				VBox {
					spacing: 5
				    content: [
						mediaView,
						progressBar
					]
				}
			]
		}
	}
	
	public function play() {
	    player.play()
	}
	
	public function setVolume(vol:Number) {
	    volume = vol;
	}
	
	public function setVideo(pVideoSrc:String) {
	    videoSrc = pVideoSrc;
	    println("Setting Video to: {pVideoSrc}");
	    println(media.duration);
	    player.play();
	}
	
	
	public override function getPrefHeight(height):Number {
		height
	}
	
	public override function getPrefWidth(width):Number {width
	}	
}